<template>
  <div>
    <div style="display:flex;justify-content:space-between">
      <p class="info">备注</p>
      <a-button type="primary" @click="visible = true" v-action:add>新增备注</a-button>
      <a-modal v-model="visible" @ok="submit">
        <a-form-model ref="form" :model="form" :rules="rules" :hideRequiredMark="true">
          <a-form-model-item label="备注内容" prop="content">
            <a-textarea class="text" :rows="4" v-model="form.content" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </div>
    <a-table rowKey="id" :columns="columns" :data-source="data">
      <span slot="createTime" slot-scope="createTime">{{ humanizeDate(createTime) }}</span>
      <span slot="operation" slot-scope="_, record">
        <edit :record="record" @update="getData" class="mr-2" v-action:update></edit>
        <!-- <a-divider type="vertical" /> -->
        <a-popconfirm title="确定删除吗" @confirm="() => onDelete(record.id)">
          <a href="javascript:;" v-action:delete>删除</a>
        </a-popconfirm>
      </span>
    </a-table>
  </div>
</template>
<script>
import { addRemarks, getRemarks, deleteRemarks } from '@/api/common'
import moment from 'moment'
import { commonRule } from '@/utils/rules'
const columns = [
  { title: '备注日期', dataIndex: 'createTime', key: 'createTime', scopedSlots: { customRender: 'createTime' } },
  { title: '备注人', dataIndex: 'sysUserName', key: 'sysUserName' },
  { title: '备注内容', dataIndex: 'content', key: 'content' }
  // { title: '操作', dataIndex: 'operation', key: 'operation', scopedSlots: { customRender: 'operation' } }
]
export default {
  name: 'remarks',
  components: {
    edit: () => import('../com/changeEdit.vue')
  },
  data() {
    return {
      visible: false,
      form: {
        content: ''
      },
      rules: {
        content: commonRule
      },
      data: []
    }
  },
  computed: {
    columns() {
      if (this.$auth('manageList.update') || this.$auth('manageList.delete')) {
        return [
          ...columns,
          {
            title: '操作',
            dataIndex: 'operation',
            key: 'operation',
            scopedSlots: { customRender: 'operation' }
          }
        ]
      } else {
        return columns
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    submit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          var id = this.$route.query.id
          var _form = {}
          _form.projectId = id
          _form.content = this.form.content
          await addRemarks(_form)
          this.form.content = ''
          this.getData()
          this.visible = false
        }
      })
    },
    async onDelete(id) {
      await deleteRemarks(id)
      this.$message.info('已删除')
      this.getData()
    },
    humanizeDate(v) {
      if (v) {
        return moment(v * 1000).format('YYYY-MM-DD') || '--'
      } else {
        return '-'
      }
    },
    async getData() {
      var res = await getRemarks(this.$route.query.id)
      this.data = res.result.data
    }
  }
}
</script>
<style lang="css" scoped>
.info {
  border-left: 2px solid #1890ff;
  padding-left: 10px;
  font-size: 25px;
  /* margin-left: 50px; */
  margin: 20px 0;
  margin-left: 50px;
}
.text {
  resize: unset;
  vertical-align: top;
}
</style>
